package com.zx.tradeclient;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;

import com.zx.base.ctrl.BaseGroup;
import com.zx.base.ctrl.Constant;
import com.zx.base.util.ActivityTaskManager;
import com.zx.base.util.Update;
import com.zx.tradeclient.app.TradeApp;
import com.zx.tradeclient.ctrl.index1.Index1Activity;
import com.zx.tradeclient.ctrl.index2.Index2Activity;
import com.zx.tradeclient.ctrl.index3.Index3Activity;
import com.zx.tradeclient.ctrl.index4.Index4Activity;
import com.zx.tradeclient.ctrl.index5.UserCenterActivity;
import com.zx.tradeclient.ctrl.index6.Index6Activity;
import com.zx.tradeclient.entity.CategoryInfo;

public class MainActivity extends BaseGroup {

	private ImageButton ib_index1; //
	private ImageButton ib_index2; //
	private ImageButton ib_index3; //
	private ImageButton ib_index4; //
	private ImageButton ib_index5; //
	private ImageButton ib_index6;

	private TradeApp app;

	private int checkButtonId;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		init();
		changeBackgroundResource(ib_index1.getId());
		setTargetIntent(getCheckedButtonId());
		launchNewActivity(targetIntent);
		if (Constant.debug)
			Log.d("debug", "onCreate");

		if (!(ActivityTaskManager.getInstance().containsName("MainActivity"))) {
			ActivityTaskManager.getInstance().putActivity("MainActivity", this);
		}

	}

	@Override
	protected void onResume() {
		if (Constant.debug)
			Log.d("debug", "onResume");
		super.onResume();
	}

	@Override
	protected void onNewIntent(Intent intent) {
		int flag = intent.getIntExtra("flag", 0);
		if (Constant.debug)
			Log.d("debug", "flag" + flag);
		switch (flag) {
		case 0:
			ib_index1.performClick();
			break;
		case 1:
			ib_index1.performClick();
			break;
		case 2:
			ib_index2.performClick();
			break;
		case 3:
			ib_index3.performClick();
			break;
		case 4:
			ib_index4.performClick();
			break;
		case 5:
			ib_index5.performClick();
			break;
		case 6:
			ib_index6.performClick();
			break;
		default:
			ib_index1.performClick();
			break;
		}
		super.onNewIntent(intent);
	}

	public void onDestroy() {

		super.onDestroy();
		ActivityTaskManager.getInstance().closeAllActivity();
	}

	/*
	 * 初始化底部条按钮
	 */
	private void init() {

		app = (TradeApp) getApplication();

		Update up = new Update(MainActivity.this, app, false);
		up.checkUpdate();

		ib_index1 = (ImageButton) findViewById(R.id.main_ib_index1);
		ib_index1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				changeBackgroundResource(ib_index1.getId());
				setTargetIntent(getCheckedButtonId());
				launchActivity(targetIntent);
			}
		});

		ib_index2 = (ImageButton) findViewById(R.id.main_ib_index2);
		ib_index2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				app.product_rating = new CategoryInfo();
				app.categ_index = 2;

				if (!((TradeApp) getApplicationContext()).isNetWorkConnection) { // 判断当前联网状态是否可用
					Toast toast = Toast.makeText(MainActivity.this, "当前网络不可用!  无法登录操作", Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				}/*
				 * else if (app.user.getId() == null ||
				 * app.user.getId().equals("")) {
				 * 
				 * Intent intent = new Intent(MainActivity.this,
				 * LoginActivity.class); startActivity(intent); }
				 */else {
					changeBackgroundResource(ib_index2.getId());
					setTargetIntent(getCheckedButtonId());
					launchActivity(targetIntent);
				}
			}
		});

		ib_index3 = (ImageButton) findViewById(R.id.main_ib_index3);
		ib_index3.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				app.product_rating = new CategoryInfo();
				app.categ_index = 3;

				if (((TradeApp) getApplicationContext()).isNetWorkConnection) { // 判断当前联网状态是否可用
					changeBackgroundResource(ib_index3.getId());
					setTargetIntent(getCheckedButtonId());
					launchActivity(targetIntent);
				} else {
					Toast toast = Toast.makeText(MainActivity.this, "当前网络不可用!  无法查看最新资讯", Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				}
			}
		});

		ib_index4 = (ImageButton) findViewById(R.id.main_ib_index4);
		ib_index4.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				if (((TradeApp) getApplicationContext()).isNetWorkConnection) { // 判断当前联网状态是否可用
					changeBackgroundResource(ib_index4.getId());
					setTargetIntent(getCheckedButtonId());
					launchActivity(targetIntent);
				} else {
					Toast toast = Toast.makeText(MainActivity.this, "当前网络不可用!  无法登录操作", Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				}

			}
		});

		ib_index5 = (ImageButton) findViewById(R.id.main_ib_index5);
		ib_index5.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				app.product_rating = new CategoryInfo();
				app.categ_index = 5;

				if (!((TradeApp) getApplicationContext()).isNetWorkConnection) { // 判断当前联网状态是否可用
					Toast toast = Toast.makeText(MainActivity.this, "当前网络不可用!  无法登录操作", Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				} else {
					changeBackgroundResource(ib_index5.getId());
					setTargetIntent(getCheckedButtonId());
					launchActivity(targetIntent);
				}
			}
		});

		ib_index6 = (ImageButton) findViewById(R.id.main_ib_index6);
		ib_index6.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				changeBackgroundResource(ib_index6.getId());
				setTargetIntent(getCheckedButtonId());
				launchActivity(targetIntent);
			}
		});

	}

	private void changeBackgroundResource(int id) {
		checkButtonId = id;
		ib_index1.setBackgroundResource(R.drawable.bottom_index1);
		ib_index2.setBackgroundResource(R.drawable.bottom_index2);
		ib_index3.setBackgroundResource(R.drawable.bottom_index3);
		ib_index4.setBackgroundResource(R.drawable.bottom_index4);
		ib_index5.setBackgroundResource(R.drawable.bottom_index5);
		ib_index6.setBackgroundResource(R.drawable.bottom_index6);
		if (id == ib_index1.getId()) {
			ib_index1.setBackgroundResource(R.drawable.bottom_index1_press);
		} else if (id == ib_index2.getId()) {
			ib_index2.setBackgroundResource(R.drawable.bottom_index2_press);
		} else if (id == ib_index3.getId()) {
			ib_index3.setBackgroundResource(R.drawable.bottom_index3_press);
		} else if (id == ib_index4.getId()) {
			ib_index4.setBackgroundResource(R.drawable.bottom_index4_press);
		} else if (id == ib_index5.getId()) {
			ib_index5.setBackgroundResource(R.drawable.bottom_index5_press);
		} else if (id == ib_index6.getId()) {
			ib_index6.setBackgroundResource(R.drawable.bottom_index6_press);
		}
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return getCurrentActivity().onKeyDown(keyCode, event);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Class<? extends Activity>[] getClasses() {
		Class<? extends Activity>[] classes = new Class[] { Index1Activity.class, Index2Activity.class, Index4Activity.class, Index3Activity.class,
				UserCenterActivity.class, Index6Activity.class };
		return classes;
	}

	@Override
	protected int[] getNavIds() {
		return new int[] { R.id.main_ib_index1, R.id.main_ib_index2, R.id.main_ib_index3, R.id.main_ib_index4, R.id.main_ib_index5, R.id.main_ib_index6 };
	}

	@Override
	protected int getLayoutResourceId() {
		return R.layout.main_activity;
	}

	@Override
	protected int getCheckedButtonId() {
		return checkButtonId;
	}
}
